package org.halk.mq;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.halk.mq.entity.JobRequest;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * @Author halk
 * @Date 2023/8/21 15:59
 */
public class SendHello2 {

    public static final String PRODUCER_GROUP = "biims-group";
    public static final String DEFAULT_NAMESRVADDR = "127.0.0.1:9876";
    public static final String TOPIC = "Biims_Job_Req";

    public static void main(String[] args) throws MQClientException, InterruptedException, MQBrokerException, RemotingException, IOException {
        DefaultMQProducer producer = new DefaultMQProducer(PRODUCER_GROUP);
        producer.setNamesrvAddr(DEFAULT_NAMESRVADDR);
        producer.setSendMsgTimeout(6000);
        producer.start();

        JobRequest jobRequest = new JobRequest()
                .setId("biims-clctdash.statisticsSiteClctActual")
                .setTimeout(120)
                .setMsgId("this is test msgid")
//                .setParams(new Object[]{"nihao", "BNU"})
                .setServer("biims-clctdash");

        Message msg = new Message();
        msg.setTopic(TOPIC);
        msg.setBody(JSON.toJSONString(jobRequest).getBytes(StandardCharsets.UTF_8));
        producer.send(msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送成功！" + sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        });

//        RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
//        rocketMQTemplate.setProducer(producer);


//        for (int i = 0; i < 10; i++) {
//            Hello message = new Hello("韩跑跑" + i, 210 + i, new Date());
//            SendResult sendResult = rocketMQTemplate.syncSend(TOPIC, JSON.toJSONString(message));
//            System.out.println("发送消息：" + JSON.toJSONString(message) + " 发送结果：" + sendResult);
//
//            Thread.sleep(3000);
//        }

//        producer.shutdown();
        System.out.println("发送成功！");

        System.in.read();
    }

}
